package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.JwtHelper;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/user/userinfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @ApiOperation(value = "用户登录接口")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) {
        // 调用service层中的登录方法
        Map<String, Object> map = userInfoService.login(loginVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/info")
    public R getUserInfo(@RequestHeader String token) {
        // 从token中解析出用户的id
        Long userId = JwtHelper.getUserId(token);
        UserInfo userInfo = userInfoService.getUserById(userId);
        return R.ok().data("userInfo", userInfo);
    }

    @ApiOperation(value = "实名认证")
    @PostMapping("/update")
    public R userAuth(@RequestHeader String token, @RequestBody UserAuthVo userAuthVo) {
        Long userId = JwtHelper.getUserId(token);
        boolean flag = userInfoService.userAuth(userId, userAuthVo);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }

}

